package com.fwzc.giuseppe.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class BitmapUtils {

	/**
	 * 转换图片成圆形
	 * 
	 * @param bitmap
	 *            传入Bitmap对象
	 * 如果是 width <= height时，则会裁剪高度，裁剪的区域是宽度不变高度从顶部到宽度width的长度；
	 * 如果 width > height，则会裁剪宽度，裁剪的区域是高度不变，宽度是取的图片宽度的中心区域
	 * @return
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			float clip = (height - width) / 2;
			top = clip;
			bottom = height - clip;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}
		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int whiteColor = 0xff424242;
		final Paint paint = new Paint();
		//整个被截取的图片
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		//整个矩形
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);
		
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(whiteColor);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, rectF, paint);
		return output;
	}
	
	/**
	 * 画出指定圆的直径大小的圆
	 * @param bitmap
	 * @param round 圆的直径
	 * @return
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap,int round) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			if(round <= 0 || round > width){
				round = width;
			}
			roundPx = width / 2;
			float clip = (height - round) / 2;
			top = clip;
			bottom = height - clip;
			left = 0;
			right = width;
			height = round;
			dst_left = 0;
			dst_top = 0;
			dst_right = round;
			dst_bottom = round;
		} else {
			if(round <= 0 || round > height){
				round = height;
			}
			roundPx = height / 2;
			float clip = (width - round) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = round;
			dst_left = 0;
			dst_top = 0;
			dst_right = round;
			dst_bottom = round;
		}
		Bitmap output = Bitmap.createBitmap(round, round, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int whiteColor = 0xff424242;
		final Paint paint = new Paint();
		//整个被截取的图片
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		//整个矩形
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);
		
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(whiteColor);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, rectF, paint);
		return output;
	}
}
